


#include <LPC23xx.H>
#include <stdio.h>
#include "audio.h"
#include "LCD.h"


char lcd_text[2][16+1] = {	"Notni generator:",       /* Buffer for LCD text         */
                     				" "};

unsigned char ofset = 0;


/* This function is called from int aud_init(void)
 */
void gpio_InitAudioPins(void)
{

  	PINSEL10 = 0x00000000; // disable ETM
  	SCS |= 0x00000001; // Enable enhanced high speed access to P0 and P1

/* DAC */
  PINSEL1 |= 0x00200000;

}

void upd_display () {
   /* Update LCD Module display text. */

   lcd_clear ();
   lcd_print (lcd_text[0]);
   set_cursor (0,1);
   lcd_print (lcd_text[1]);
}


unsigned long kbd (void)
{
unsigned long tipke, m = 0x00000001;
unsigned char i;

	tipke = (~(FIO4PIN & 0x000007ff))&0x000007ff;

	for (i=0; i<11; i++)
	{
		if (m&tipke)
		{
			aud_active_note = i+2+ofset;
			break;
		}
		m<<=1;	
	}

	return tipke;
}


int main (void)

{
	unsigned char i;
	char nstr[5];
	unsigned long tipke;

	aud_init();
	aud_active_note = 0;
	aud_pitch = AUD_MID;

  	lcd_init();
	upd_display ();
	FIO4SET = 0x000007ff;

	if (kbd()>0) ofset = aud_active_note;

	while (1)
	{
		tipke = kbd();
		if (tipke > 0) 
		{
			// Init tone generator tables
			//aud_InitToneCoefArray1(tipke);

			aud_note_level = 10;
			aud_TurnOnNote();
			for (i=0; i<4; i++) nstr[i] = notacija[aud_active_note][i];
			nstr[4] = 0;	
			sprintf(lcd_text[0], "Nota=%3s, O=%u ", nstr, ofset);
			sprintf(lcd_text[1], "f = %6.3f Hz ", AUD_FREQ1[aud_active_note][0]);
			upd_display ();
			while (tipke == kbd());
		} else {
			aud_note_on = 0;
			sprintf(lcd_text[0], "Nota=---, O=%u ", ofset);
			sprintf(lcd_text[1], "   --- OFF ---");
			upd_display ();
			while (kbd() == 0);
		}
		
	}


}
